﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using QuanCafe.BusinessLayer;

namespace QuanCafe.Admin
{
    public partial class AdminLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    if (Request.Cookies["AccountLogin"] != null)
                        this.txtUsername.Text = Server.HtmlEncode(Request.Cookies["AccountLogin"].Value);
                    if (Request.Cookies["AccountPass"] != null)
                    {
                        this.txtPassword.Text = Server.HtmlEncode(Request.Cookies["AccountPass"].Value);
                        this.txtPassword.Attributes.Add("value", Server.HtmlEncode(Request.Cookies["AccountPass"].Value));
                    }
                }
                catch (Exception exp)
                {
                    lblErr.Text = exp.Message;
                }
            }
            btnLogin.Attributes.Add("OnClick", "return CheckForm();");
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            CheckAdmin CheckA = new CheckAdmin();
            if (this.txtUsername.Text != "" && this.txtPassword.Text != "")
            {
                CheckA.Username = this.txtUsername.Text.ToString();
                CheckA.Password = this.txtPassword.Text.ToString();
                try
                {
                    bool isTopAdministrator = false;
                    if (CheckA.CheckAccount(ref isTopAdministrator) == true)
                    {
                        Session["AdminName"] = this.txtUsername.Text;
                        Session["isTopAdministrator"] = isTopAdministrator;

                        if (Response.Cookies["AccountLogin"] != null)
                        {
                            HttpCookie aCookie = new HttpCookie("AccountLogin", this.txtUsername.Text);
                            aCookie.Expires = DateTime.Now.AddMonths(1);
                            Response.Cookies.Add(aCookie);
                            aCookie = null;
                        }
                        else
                        {
                            Response.Cookies["AccountLogin"].Value = this.txtUsername.Text;
                            Response.Cookies["AccountPass"].Value = this.txtPassword.Text;
                        }

                        Response.Redirect("/Admin/News.aspx");
                    }
                    else
                    {
                        lblErr.Text = "Kiểm tra lai UserName & Password";
                    }
                }
                catch (Exception ex)
                {
                    lblErr.Text = ex.Message;
                }
            }
            else
            {
                lblErr.Text = "Đăng nhập lại không đươc để trống";
            }
        }
    }
}
